package com.canbot.u05.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.canbot.u05.R;
import com.canbot.u05.activity.guide.GuideActivity;
import com.canbot.u05.activity.presidemode.HostModeListViewActivity;
import com.canbot.u05.activity.reception.ReceptionModeSettingActivity;
import com.canbot.u05.activity.shoppingGuide.sales.SaleEnterActivity;
import com.canbot.u05.activity.reception.ReceptionEnterActivity;
import com.canbot.u05.api.U05RobotManger;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.ConstUtils;
import com.canbot.u05.utils.FileUtil;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.PathConst;
import com.canbot.u05.utils.Utils;

/**
 * 手持pad 应用设置界面
 **/
public class IndustryModActivity extends BaseActivityNoTitle {

        private static final String TAG = "IndustryModActivity";

        Bitmap[] bitmap = new Bitmap[FileUtil.getFilesLength(ConstUtils.SD_PathStartrecive,
                ".png")];

        StringBuffer micPath = new StringBuffer();

        String path_content = "u5_voicereceiveanim";

        private int i;

        private static final int PAGECHANGE = 1;

        private static final int STARTRECODER = 2;

        private static final int STARTRECOGNIZER = 3;

        int rdImgCount;

        int rgImgCount;

        // 显示动画
        private ImageView merge;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.act_industry_mod);
                merge = (ImageView) findViewById(R.id.imgview);
                rdImgCount = FileUtil.getFilesLength(ConstUtils.SD_UuimagestartPath,
                        ".png");
                rgImgCount = FileUtil.getFilesLength(ConstUtils.SD_PathStartrecognize,
                        ".png");

                Logger.d(TAG, "onCreate");
                U05RobotManger.getInstance().playSound(PathConst.PAD_MODE_DIR + "yyy_01.wav");
        }

        @Override
        protected void onResume() {
                super.onResume();
                setAnimeImage(ConstUtils.SD_PathStartrecive, 2, 60);
//		LocalBroadcastManager.newInstance(this).registerReceiver(receiver, intentFilter);
//        MsgSendUtils.sendStringMsg(MsgType.ACTION, "10106");//先注释掉，方便测试
                MsgSendUtils.sendStringMsg(MsgType.EYE_MOTION, "14");

        }

        protected void startotherActivity(Context context, Class<?> cls) {
                Intent intent = new Intent(context, cls);
                startActivity(intent);
                overridePendingTransition(R.anim.face_zoom_enter,
                        R.anim.face_zoom_exit);
        }

        public void backIndustry(View view) {
                Log.d("View", " back ");
                //停止播放音频
                MsgSendUtils.sendStringMsg(MsgType.STOP_SOUND, "stop_sound");
                finish();
        }


        public void office(View view) {
                Log.d("View", " office ");
                startotherActivity(this, OfficeEnterActivity.class);
        }

        /**
         * 手持端直接进入设置界面，同时让3288本体端进入主持模式
         *
         * @param view
         */
        public void preside(View view) {
//                MsgSendUtils.sendStringMsg(MsgType.SEND_3288_PRESIDE_STAR_AVTIBITY, "主持人模式");
                startotherActivity(this, HostModeListViewActivity.class);
                Log.d("View", " preside ");
        }

        public void welcome(View view) {
                Log.d("View", " welcome ");
                startotherActivity(this, ReceptionModeSettingActivity.class);
        }

        public void guide(View view) {
                startActivity(new Intent(this, GuideActivity.class));
//                startActivity(new Intent(this, GuideEnterActivity.class));
                overridePendingTransition(R.anim.face_zoom_enter,
                        R.anim.face_zoom_exit);
        }

        /**
         * 导购Icon
         *
         * @param view
         */
        public void sale(View view) {

                startActivity(new Intent(this, SaleEnterActivity.class));
                overridePendingTransition(R.anim.face_zoom_enter,
                        R.anim.face_zoom_exit);
        }



        @Override
        protected void onPause() {
//		LocalBroadcastManager.newInstance(this).unregisterReceiver(receiver);
                mHandler.removeMessages(0);
                super.onPause();

        }

        /**
         * 添加动画
         */
        private Handler mHandler = new Handler() {
                public void handleMessage(android.os.Message msg) {
                        setAnimeImage(ConstUtils.SD_PathStartrecive,
                                STARTRECOGNIZER, 60);
                }
        };

        /**
         * 设置动画图片
         */
        private void setAnimeImage(String imgDir, int stateImage, long time) {
                Log.e("setAnimeImage", "setAnimeImage" + i);
                if (bitmap != null && merge != null) {
                        i++;
                        if (i >= bitmap.length) {
                                i = 0;
                        }
//            if (i < bitmap.length) {
                        if (micPath != null && micPath.length() > 0) {
                                micPath.delete(0, micPath.length());
                        }
                        micPath.append(path_content).append(i).append(".png");
                        bitmap[i] = Utils.fromSD(imgDir, micPath.toString());
                        micPath.delete(0, micPath.length());
                        if (bitmap[i] != null) {
                                merge.setImageBitmap(bitmap[i]);
                                Logger.d("TAG", "Bitmap[i]" + bitmap[i].toString());
                        }
                        else {
                                Logger.d("TAG", "Bitmap[i]---null");
                        }
                        if (i > 1) {
                                if (null != bitmap[(i - 1)]) {
                                        bitmap[(i - 1)].recycle();
                                        bitmap[(i - 1)] = null;
                                }
                        }
                        mHandler.sendEmptyMessageDelayed(0, time);
//            }
                }
        }


}
